import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { fetchValidation } from '@/utils';
import {
  queryProblemList,
  queryProblemCount,
  queryProblemStatus,
  cancelDelivery,
  confirmDelivery,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchCount: Effect;
    fetchProblemStatus: Effect,
    cancel: Effect;
    confirm: Effect;
  };
  reducers: {
    save: Reducer;
    saveStatus: Reducer;
    cleanCount: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'logisCenterIssueList',

  state: {
    data: {
      content: [],
      count: 0,
    },
    statusList: [],
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const { pageIndex, ...query } = payload.params;
      const response = yield call(queryProblemList, query);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'list',
          value: {
            current: pageIndex,
            search: payload.search,
            list: data.body.list,
            maxId: payload.maxId,
            minId: payload.minId,
          },
        },
      });
    },

    * fetchCount({ payload }, { call, put }) {
      const response = yield call(queryProblemCount, payload);
      const count = fetchValidation(response);
      yield count && put({
        type: 'save',
        payload: {
          type: 'count',
          value: count.body && count.body,
        },
      });
    },

    * fetchProblemStatus({ payload }, { call, put }) {
      const response = yield call(queryProblemStatus, payload);
      const data = fetchValidation(response);
      if (data) {
        const { body = {} } = data;
        const statusArrs = Object.keys(body)
          .map((item: string) => ({ key: item, value: body[item] }));
        yield data && put({
          type: 'saveStatus',
          payload: statusArrs,
        });
      }
    },

    * confirm({ payload: { search: params, ...query } }, { call, put }) {
      const response = yield call(confirmDelivery, query);
      const data = fetchValidation(response);
      // if (data) {
      message.success('操作成功');
      yield put({
        type: 'fetch',
        payload: { params },
      });
      // }
    },
    * cancel({ payload: { search: params, ...query } }, { call, put }) {
      const response = yield call(cancelDelivery, query);
      const data = fetchValidation(response);
      // if (data) {
      message.success('操作成功');
      yield put({
        type: 'fetch',
        payload: { params },
      });
      // }
    },
  },

  reducers: {
    save(state, { payload }) {
      const { data } = state;
      if (payload.type === 'list') {
        const pos = data.content.findIndex((item: any) => item.current === payload.value.current);
        if (pos === -1) {
          data.content.push(payload.value);
        } else {
          data.content[pos] = payload.value;
        }
      } else if (payload.type === 'count') {
        data.count = payload.value;
      }
      return {
        ...state,
        data,
      };
    },

    saveStatus(state, { payload }) {
      return {
        ...state,
        statusList: payload,
      };
    },

    cleanCount(state) {
      const { data } = state;
      data.count = 0;
      return {
        ...state,
        data,
      };
    },
  },
};

export default Model;
